package com.huya.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huya.common.result.PageResult;
import com.huya.mapper.LegalMapper;
import com.huya.pojo.dto.LegalDTO;
import com.huya.pojo.dto.LegalPageQueryDTO;
import com.huya.pojo.entity.Legal;
import com.huya.service.LegalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class LegalServiceImpl  implements LegalService {

    @Autowired
    private LegalMapper legalMapper;
    /**
     * 分页查询法律法规
     * @param legalPageQueryDTO
     * @return
     */
    public PageResult pageQuery(LegalPageQueryDTO legalPageQueryDTO) {
        PageHelper.startPage(legalPageQueryDTO.getPage(), legalPageQueryDTO.getPageSize());
        Page<Legal> page = legalMapper.pageQuery(legalPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }


    /**
     * 根据id查询法律法规
     * @param id
     * @return
     */
    public Legal getLegalById(Long id) {
        return legalMapper.selectByLegalId(id);
    }

    /**
     * 批量删除法律法规
     * @param ids
     */
    public void delete(List<Long> ids) {
        legalMapper.deleteByLegalIds(ids);
    }


    /**
     * 更新法律法规
     * @param legalDTO
     */
    public void update(LegalDTO legalDTO) {
        Legal legal = new Legal();
        BeanUtils.copyProperties(legalDTO, legal);
        legal.setUpdateTime(LocalDateTime.now());
        legalMapper.update(legal);
    }


    /**
     * 新增法律法规
     * @param legalDTO
     */
    public void add(LegalDTO legalDTO) {
        Legal legal = new Legal();
        BeanUtils.copyProperties(legalDTO, legal);
        legal.setUpdateTime(LocalDateTime.now());
        legalMapper.insert(legal);
    }
}
